package com.hq.modeapp.ui;

import java.util.ArrayList;

import net.tsz.afinal.http.AjaxCallBack;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;
import org.taptwo.android.widget.ViewFlow.ViewSwitchListener;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hq.modeapp.R;
import com.hq.modeapp.bean.Photo_Bean;
import com.hq.modeapp.bean.Photo_List_Bean;
import com.hq.modeapp.bean.Photo_Specific_Bean;
import com.hq.modeapp.manager.AppContext;
import com.hq.modeapp.tools.Tools;

public class PhotoViewFlowActivity extends Activity {
	private TextView tv_brief;// 简介
	private TextView tv_counts;// 页数
	private TextView tv_contents;// 内容
	private Context context;
	private String ID;
	private ViewFlow viewflow;
	private Photo_Specific_Bean bean;
	private int count = 0;
	private int size;
	private boolean isVisible;
	private boolean isVisible2;
	private View includeViewTop;
	private View includeViewBottom;
	private RelativeLayout rl_title;
	private LinearLayout ll_content;
	private ImageView go_back;
	private RelativeLayout rl_all;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.photoext_gallery_activity);
		initComponent();
		registerListener();
		initData();
	}

	public void initViewFlow() {
		tv_counts.setText((1) + "/" + bean.getImgsum());
		tv_brief.setText(bean.getSetname());
		tv_contents.setText(bean.getPhotos().get(0).getNote());
		viewflow = (ViewFlow) findViewById(R.id.viewflow);
		viewflow.setAdapter(new ImageAdapter(this, bean.getPhotos()));
		CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
		viewflow.setFlowIndicator(indic);
		// 当viewFlow中设置了setSelection()或者在进行切换到另一个画面是会进行监听，调用setOnViewSwitchListener监听方法
		viewflow.setOnViewSwitchListener(new ViewSwitchListener() {

			@Override
			public void onSwitched(View view, int position) {
				// TODO Auto-generated method stub
				System.out.println("view位置===" + position);
				tv_counts.setText((position + 1) + "/" + bean.getImgsum());
				tv_brief.setText(bean.getSetname());
				tv_contents.setText(bean.getPhotos().get(position).getNote());
			}
		});

	}

	public void initData() {

		String url = "http://c.3g.163.com/photo/api/set/0096/" + ID + ".json";
		((AppContext) getApplicationContext()).finalHttp.get(url,
				new AjaxCallBack<String>() {

					@Override
					public void onSuccess(String t) {
						// TODO
						// Auto-generated
						// method stub
						super.onSuccess(t);
						System.out.println("网易新闻详细列表==" + t.toString());
						bean = Photo_Specific_Bean.parseJson(t);
						initViewFlow();
					}

					@Override
					public void onFailure(Throwable t, String strMsg) {
						// TODO
						// Auto-generated
						// method stub
						super.onFailure(t, strMsg);
						System.out.println("网易新闻详细列表" + strMsg);
						Tools.getInstance().showTextToast(
								PhotoViewFlowActivity.this, "网络异常！");
						// 诉它数据加载完毕;
						// mPullDownView.notifyDidLoad();
					}
				});

	}

	public void initComponent() {
		context = PhotoViewFlowActivity.this;
		includeViewBottom = findViewById(R.id.include_bottom);
		tv_brief = (TextView) includeViewBottom.findViewById(R.id.tv_brief);
		tv_counts = (TextView) includeViewBottom.findViewById(R.id.tv_counts);
		tv_contents = (TextView) includeViewBottom
				.findViewById(R.id.tv_contents);

		Intent myIntent = getIntent();
		ID = myIntent.getStringExtra("ID");
		rl_title = (RelativeLayout) includeViewBottom
				.findViewById(R.id.rl_title);
		ll_content = (LinearLayout) includeViewBottom
				.findViewById(R.id.ll_content);
		includeViewTop = findViewById(R.id.include_top);
		go_back = (ImageView) includeViewTop.findViewById(R.id.go_back);
	}

	public void registerListener() {
		rl_all = (RelativeLayout) findViewById(R.id.rl_all);
		// rl_all.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if (isVisible ) {
		// // photoext_title_layout_top.setVisibility(View.GONE);
		// // photoext_title_layout_bottom.setVisibility(View.GONE);
		// includeViewTop.setVisibility(View.GONE);
		// includeViewBottom.setVisibility(View.GONE);
		// isVisible = false;
		//
		// } else if (!isVisible ) {
		// // photoext_title_layout_top.setVisibility(View.VISIBLE);
		// // photoext_title_layout_bottom.setVisibility(View.VISIBLE);
		//
		// includeViewTop.setVisibility(View.VISIBLE);
		// includeViewBottom.setVisibility(View.VISIBLE);
		// isVisible = true;
		// }
		//
		//
		//
		// }
		// });
		rl_title.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (isVisible2) {
					ll_content.setVisibility(View.GONE);
					isVisible2 = false;
				} else {
					ll_content.setVisibility(View.VISIBLE);
					isVisible2 = true;
				}

			}
		});

		go_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();

			}
		});

	}

	class ImageAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private ArrayList<Photo_Bean> list;
		private Context context;

		public ImageAdapter(Context context, ArrayList<Photo_Bean> list) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// if (convertView == null) {
			// convertView = mInflater.inflate(R.layout.image_item, null);
			// }
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = new PhotoView(parent.getContext());
				holder.photoView = (PhotoView) convertView;

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.photoView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					System.out
							.println("======photoView.setOnClickListener=======");
				}
			});
			holder.photoView.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			holder.photoView.setOnPhotoTapListener(new OnPhotoTapListener() {// 单击事件

						@Override
						public void onPhotoTap(View view, float x, float y) {
							// TODO Auto-generated method stub
							if (rl_all.getVisibility() == View.VISIBLE) {
								rl_all.setVisibility(View.GONE);
							} else if (rl_all.getVisibility() == View.GONE) {
								rl_all.setVisibility(View.VISIBLE);
							}

						}
					});
			// ImageView photoView = new ImageView(parent.getContext());
			((AppContext) context.getApplicationContext()).finalBitmap.display(
					holder.photoView, list.get(position).getImgurl());
			return convertView;

			// Now just add PhotoView to ViewPager and return it
			// container.addView(photoView, LayoutParams.MATCH_PARENT,
			// LayoutParams.MATCH_PARENT);

			// ((ImageView)
			// convertView.findViewById(R.id.imgView)).setImageResource(ids[position]);
			// return convertView;
		}

		class ViewHolder {
			PhotoView photoView;
		}
	}

}
